package com.it.common.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.common.domain.dos.CategoryDO;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

public interface CategoryMapper extends BaseMapper<CategoryDO> {

    CategoryDO selectByName(String categoryName);

    Boolean deleteCategory(Long id);

    List<CategoryDO> findCategorySelectList();


    /**
     * 分页查询
     */
    default Page<CategoryDO> selectPageList(long current, long size, String name, LocalDate startDate, LocalDate endDate){
        Page<CategoryDO> page = new Page<>(current, size);
        LambdaQueryWrapper<CategoryDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Objects.nonNull(name), CategoryDO::getName, name)
                .ge(Objects.nonNull(startDate),CategoryDO::getCreateTime,startDate)
                .le(Objects.nonNull(endDate),CategoryDO::getCreateTime,endDate)
                .orderByDesc(CategoryDO::getCreateTime);

        return selectPage(page, wrapper);
    }


    /**
     * 查询分类总数
     * @return {@link Long }
     */
    Long selectCategoryTotal();

    /**
     * 查询时指定数量
     * @param limit
     * @return
     */
    default List<CategoryDO> selectByLimit(Long limit) {
        return selectList(Wrappers.<CategoryDO>lambdaQuery()
                .orderByDesc(CategoryDO::getArticlesTotal) // 根据文章总数降序
                .last(String.format("LIMIT %d", limit))); // 查询指定数量
    }
}
